#include "CFPS.h"

#define FPS 60
Uint32 waittime = 1000.0f/FPS;
Uint32 framestarttime = 0;
Sint32 delaytime;

CFPS CFPS::FPSControl;

CFPS::CFPS() {
    OldTime     = 0;
    LastTime    = 0;

    SpeedFactor    = 0;

    Frames      = 0;
    NumFrames   = 0;
}

void CFPS::OnLoop() {
    if(OldTime + 1000 < SDL_GetTicks()) {
        OldTime = SDL_GetTicks();

        NumFrames = Frames;

        Frames = 0;
    }

    SpeedFactor = ((SDL_GetTicks() - LastTime) / 1000.0f) * 32.0f;

    LastTime = SDL_GetTicks();

    Frames++;
}

int CFPS::GetFPS() {
    return NumFrames;
}

float CFPS::GetSpeedFactor() {
    return SpeedFactor;
}

void CFPS::ShowFPS(SDL_Surface* Surf_Src, TTF_Font* Font, int X, int Y) {
    char Buffer[255];
    sprintf(Buffer, "%d", CFPS::FPSControl.GetFPS());
    CFont::OnDraw(Font, 1, Buffer, Surf_Src, X, Y);
}

void CFPS::Delay() {
    delaytime = waittime - (SDL_GetTicks() - framestarttime);
    if(delaytime > 0)
        SDL_Delay((Uint32)delaytime);
    framestarttime = SDL_GetTicks();
}
